package com.hz.framework.dao;

import java.io.Serializable;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import com.hz.framework.util.SqlParameter;
import com.hz.sessionFactory.HibernateSessionFactory;

/**
 * *****************************************************************************
 *               Copyright(c) 2010 by 哈尔滨华泽数码科技有限公司.
 *                       All rights reserved.
 *******************************************************************************
 *     File Name         :  com.hz.framework.dao.CommonIDU.java
 *     Description(说明)	: 公共增删改查方法类 包含增删该各一个方法，参数传入类对象即可。 
 *     						包含根据传入参数进行拼装HQL语句进行删除和更新的方法
 * 						包含一个根据所传参数进行拼装HQL的查询方法和一个传入HQL语句进行查询的方法
 * -----------------------------------------------------------------------------
 *     No.        Date              Revised by           Description
 *     @param <T>	  May 19, 2011           HTT              Created
 *****************************************************************************
 */
public class CommonSel<T> extends CommonIDU {

	private Logger logger = Logger.getLogger(CommonSel.class);
	
	private List lstValue = null;
	private Session session = null;
	private Transaction tx = null;

	public CommonSel() {
	}

	// 传HQL数组查询出，返回值必须是一样的.返回的结果是多个SQL语句加起来返回的结果
	public List FindHqlplural(String[] hql) {
		List<String> totallist = new ArrayList<String>();
		List singalist = null;
		try {
			session = HibernateSessionFactory.getSession();
			for (int i = 0; i < hql.length; i++){
				singalist = new ArrayList();
				singalist = session.createQuery(hql[i]).list();
				totallist.addAll(singalist);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return totallist;
	}

	// 标准化查询 参数: 对象 , where条件(单一) , 对应的值(单一) 返回单一对象
	public Object getStandObj(Object obj, String where, String value) {
		try {
			session = HibernateSessionFactory.getSession();
			Object hobj = session.createCriteria(obj.getClass()).add(
					Restrictions.eq(where, value)).uniqueResult();
			return hobj;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			session.close();
		}
	}
	// 标准化查询 参数: 对象 , where条件(单一) , 对应的值(单一) 返回结果集合的list对象
	public List getStandList(Object obj, String where, String value) {
		try {
			session = HibernateSessionFactory.getSession();
			lstValue = session.createCriteria(obj.getClass()).add(
					Restrictions.eq(where, value)).list();
			return lstValue;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			session.close();
		}
	}
//	 标准化查询 参数: 对象 , where条件(多条件) , 对应的值(多值) 返回结果集合的list对象 咱只支持条件匹配一种
	public List getStandList(Object obj, String where[], String value[]) {
		try {
			session = HibernateSessionFactory.getSession();
			Criteria ca = session.createCriteria(obj.getClass());
			for (int i = 0; i < where.length; i++){
				ca.add(Restrictions.eq(where[i], value[i]));
			}
			lstValue = ca.list();
			return lstValue;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			session.close();
		}
	}

	// HQL查询 参数: HQL语句 返回结果集合的list对象
	public List selHqlList(String hql) {
		lstValue = new ArrayList();
		try {
			session = HibernateSessionFactory.getSession();
			Query query = session.createQuery(hql);
			lstValue = query.list();
			return lstValue;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			session.close();
		}
	}

	// HQL查询 参数: HQL语句 返回结果的Object对象
	public Object selHqlObj(String hql) {
		try {
			session = HibernateSessionFactory.getSession();
			Query query = session.createQuery(hql);
			Object obj = query.setMaxResults(1).uniqueResult();
			return obj;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			session.close();
		}
	}
//	 SQL查询 参数: SQL语句 返回结果集合的list对象
	public List selSqlList(String sql) {
		lstValue = new ArrayList();
		try {
			session = HibernateSessionFactory.getSession();
			lstValue = session.createSQLQuery(sql).list();
			return lstValue;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			session.close();
		}
	}
	// SQL查询 参数: SQL语句 返回结果的Object对象
	public Object selSqlObj(String sql) {
		try {
			session = HibernateSessionFactory.getSession();
			Object obj = session.createSQLQuery(sql).setMaxResults(1).uniqueResult();
			return obj;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			session.close();
		}
	}
//	 SQL查询 参数: SQL语句预处理 返回结果的Object对象
	public Object selSqlObjByST(String sql,String ...params) {
		try {
			session = HibernateSessionFactory.getSession();
			Query query = session.createSQLQuery(sql);
			Object obj = null;
			if (params.length > 0) {
				for (int i = 0; i < params.length; i++) {
					// 循环为query中的hql加上参数
					query.setParameter(i, params[i]);
					if (i == params.length - 1) {
						obj = query.setMaxResults(1).uniqueResult();
					}
				}
			}
			return obj;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			session.close();
		}
	}
//	 SQL查询 参数: SQL语句预处理 返回结果的List对象
	public List selSqlListByST(String sql,String...params) {
		List list = null;
		try {
			session = HibernateSessionFactory.getSession();
			Query query = session.createSQLQuery(sql);
			if (params.length > 0) {
				for (int i = 0; i < params.length; i++) {
					// 循环为query中的hql加上参数
					query.setParameter(i, params[i]);

					if (i == params.length - 1) {
						list = query.list() ;
					}
				}
			}else{
				list = query.list() ;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			session.close();
		}
		return list;
	}
	/**
	 * 利用Hibernate预处理来处理hql语句
	 * @param hql
	 * @param params 参数数组
	 * @return Object 返回单一对象
	 */
	public Object selHqlObjByST(String hql, String ...params) {
		Object obj = null;
		try {
			session = HibernateSessionFactory.getSession();
			Query query = session.createQuery(hql);
			if (params.length > 0) {
				for (int i = 0; i < params.length; i++) {
					// 循环为query中的hql加上参数
					query.setParameter(i, params[i]);

					// 当为最后最后一个元素时候,加上uniqueResult方法,进行处理
					if (i == params.length - 1) {
						obj = query.setMaxResults(1).uniqueResult();
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			session.close();
		}
		return obj;
	}
	/**
	 * 利用Hibernate预处理来处理hql语句
	 * @param hql
	 * @param params 参数数组
	 * @return Object 返回list集合对象
	 */
	public List selHqlListByST(String hql, String ...params) {
		List list = null;
		try {
			session = HibernateSessionFactory.getSession();
			Query query = session.createQuery(hql);
			if (params.length > 0) {
				for (int i = 0; i < params.length; i++) {
					// 循环为query中的hql加上参数
					query.setParameter(i, params[i]);

					if (i == params.length - 1) {
						list = query.list() ;
					}
				}
			}else{
				list = query.list() ;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			session.close();
		}
		return list;
	}
	/**
	 * @author 韩飞
	 * @time Oct 8, 2010
	 * @param eam  类位置
	 * @param szb  主键单一值
	 * @return 方法功能: 根据主键使用get方法查询对象
	 */
	public Object hibernateGet(String eam, Serializable szb) {
		try {
			session = HibernateSessionFactory.getSession();
			Object obj = session.get(eam, szb);
			return obj;
		} catch (Exception e) {
			System.out.println("hibernateGet String 失败");
			e.printStackTrace();
			return null;
		} finally {
			session.close();
		}
	}
	/**
	 * @author 韩飞
	 * @time Oct 8, 2010
	 * @param eam  类
	 * @param szb  主键单一值
	 * @return 方法功能: 根据主键使用get方法查询对象
	 */
	public <T extends Serializable> T hibernateGet(Class<T> eam, Serializable szb) {
		try {
			session = HibernateSessionFactory.getSession();
			return (T) session.get(eam, szb);
		} catch (Exception e) {
			System.out.println("hibernateGet Class<T> 失败");
			e.printStackTrace();
			return null;
		} finally {
			session.close();
		}
	}
	/**
	 * @author 金雪冰
	 * @time Oct 10, 2010(增加)
	 * @param eam
	 * @param szb
	 * @return 方法功能: 根据条件hql查询，返回对象
	 */
	public T selHqlObjByPS(String hql,SqlParameter sqlParams) {
		try {
			session = HibernateSessionFactory.getSession();
			Query q=session.createQuery(hql);
			this.setParameters(q,sqlParams);
			return (T)q.uniqueResult();
		} catch (Exception e) {
			System.out.println("selHqlObjByPS 失败");
			e.printStackTrace();
			return null;
		} finally {
			session.close();
		}
	}
	/**
	 * @author 金雪冰
	 * @time Oct 10, 2010(增加)
	 * @param eam
	 * @param szb
	 * @return 方法功能: 根据条件sql查询，返回List集合
	 */
	public List selSqlListByPS(String sql,SqlParameter sqlParams) {
		try {
			session = HibernateSessionFactory.getSession();
			Query q = session.createSQLQuery(sql);
			this.setParameters(q,sqlParams);
			return q.list();
		} catch (Exception e) {
			System.out.println("selSqlListByPS 失败");
			e.printStackTrace();
			return null;
		} finally {
			session.close();
		}
	}
	/**
	 * @author 金雪冰
	 * @time Oct 10, 2010(增加)
	 * @param eam
	 * @param szb
	 * @return 方法功能: 根据条件hql查询，返回对象
	 */
	public T selSqlObjByPS(String sql,SqlParameter sqlParams) {
		try {
			session = HibernateSessionFactory.getSession();
			Query q = session.createSQLQuery(sql);
			this.setParameters(q,sqlParams);
			return (T)q.uniqueResult();
		} catch (Exception e) {
			System.out.println("selSqlObjByPS 失败");
			e.printStackTrace();
			return null;
		} finally {
			session.close();
		}
	}
	/**
	 * @author 金雪冰
	 * @time Oct 10, 2010(增加)
	 * @param eam
	 * @param szb
	 * @return 方法功能: 根据条件sql查询，返回List集合
	 */
	public List selHqlListByPS(String hql,SqlParameter sqlParams) {
		try {
			session = HibernateSessionFactory.getSession();
			Query q = session.createQuery(hql);
			this.setParameters(q,sqlParams);
			return q.list();
		} catch (Exception e) {
			System.out.println("selHqlListByPS 失败");
			e.printStackTrace();
			return null;
		} finally {
			session.close();
		}
	}
	/***************************************************************************
	 * 添加参数。
	 * 
	 * @param sqlParams
	 */
	private void setParameters(Query q, SqlParameter sqlParams) {
		if (sqlParams != null && sqlParams.size() > 0) {
			for (int i = 0; i < sqlParams.size(); i++) {
				q.setParameter(i, sqlParams.get(i));
				// TODO Auto-generated catch block
			}
		}
	}
	/**
	 * @author 韩飞
	 * @time May 19, 2011	
	 * @return 
	 * 方法功能: 获得session对象
	 */	
	public Session getHibernateTemplate() {
		session = HibernateSessionFactory.getSession();
		return session;
	}

	/** 条件查询,参数（表名，where条件名，request,order）order=true时增加id倒序排序,false为不排序 */
	public String whereList(String table, String[] column,
			HttpServletRequest request, boolean order) {
		if (!"".equals(table)) {
			lstValue = new ArrayList();
			String hql = "from " + table + " where 1=1";
			if (column != null) {
				for (int i = 0; i < column.length; i++) {
					if (request.getParameter(column[i]) != null
							&& !"".equals(request.getParameter(column[i]))) {
						hql += " and " + column[i] + " = '"
								+ request.getParameter(column[i]) + "'"; // 按条件查询
					}
				}
			}
			if (order) {
				hql += " order by id desc";
			}
			try {
				session = HibernateSessionFactory.getSession();
				tx = session.beginTransaction();
				Query query = session.createQuery(hql);
				lstValue = query.list();
				System.out.println(query.toString());
				tx.commit();
				logger.info("select true!return list for you");
				return hql;
			} catch (Exception e) {
				e.printStackTrace();
				if (tx != null) {
					tx.rollback();
				}
				return null;
			} finally {
				session.close();
			}
		} else {
			logger.info("select false!not table value");
			return null;
		}
	}

}
